home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 421_01 / scroll.cpp < prev    next >
C/C++ Source or Header  |  1994-01-16  |  7KB  |  343 lines

  1. // SCROLL.CPP
  2.  
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <dos.h>
  6. #include "stdio.h"
  7. #include "timer.h"
  8. #include "vx.h"
  9.  
  10. #define PI 3.14159265
  11.  
  12. extern timer_C timer;
  13. extern vx_C vx;
  14. extern byte *keypressed;
  15.  
  16. static byte *sdataseg1,*dataseg2,*bmp_ptr,*sdata_ptr;
  17.  
  18. #define BMPX        480
  19. #define BMPY            32
  20. #define CENTERX        160
  21. #define CENTERY         120
  22. #define OUTRAD          120
  23. #define RADWIDTH    58
  24. #define INRAD           (OUTRAD-RADWIDTH+1)
  25. #define SDATA        59074
  26. #define BMPDATA        (2*BMPX*BMPY)
  27.  
  28. #define FILELEN         59074
  29.  
  30. #define COLOR1        1
  31. #define COLOR2        2
  32.  
  33. // DRAWSCROLL
  34.  
  35. // offset sdat=offset bmpdat=0 !
  36.  
  37. void drawscroll(word *sdat, word *bmpdat,word twist,word vrampos)
  38. {
  39.   word nextsp=0;
  40.   long bmp_long=(long)bmpdat;
  41.   long s_long=(long)sdat;
  42.  
  43.     asm mov ax,vrampos
  44.     asm mov es,ax
  45.     asm pusha
  46.  
  47.     for (byte qplan=0; qplan<4; qplan++)
  48.     {
  49.         asm mov cl,qplan
  50.         asm mov ax,0x0100+MAP_MASK
  51.         asm shl ah,cl
  52.         asm mov dx,SC_INDEX
  53.         asm out dx,ax
  54.  
  55.         asm push ds
  56.         asm push bp
  57.  
  58.         asm lds ax,s_long
  59.         asm mov ax,ds                    // ax = dataseg (STACK)
  60.         asm lds bx,bmp_long
  61.         asm mov bx,ds
  62.         asm add bx,twist            // bx = bmp-seg
  63.         asm mov ds,bx
  64.         asm mov cx,sp                    // static: cx = sp
  65.         asm mov dx,ss                    // static: dx = ss
  66.  
  67.         asm mov bx,nextsp
  68.         asm mov ss,ax
  69.         asm mov ax,SDATA-50
  70.         asm mov sp,ax                    // sp = safe
  71.         asm push bp                        // bp = first on new stack
  72.  
  73.         asm mov bp,bx                    // ok! here we go.
  74.                                                     // Scratch registers: ax, bx, si, di
  75.  
  76.         asm mov bx,79
  77.         asm mov al,[bp]                // al = # of stripes
  78.         asm inc bp
  79.         asm jmp short next_strip
  80. alone:
  81.         asm mov si,[bp]
  82.         asm inc bp
  83.         asm inc bp
  84.         asm movsb
  85.         asm dec al
  86.         asm jz done
  87. next_strip:
  88.         asm mov di,[bp]                // di = screenpos
  89.         asm inc bp
  90.         asm inc bp
  91.         asm mov ah,[bp]                // ah = length of strip
  92.         asm inc bp
  93. // UNROLL!
  94.         asm shr ah,1
  95.         asm jnc parity
  96.         asm jz alone
  97.         asm mov si,[bp]                // si =x-ref
  98.         asm inc bp
  99.         asm inc bp
  100.         asm movsb
  101.         asm add di,bx
  102. next_pixel:
  103. parity:
  104.         asm mov si,[bp]                // si =x-ref
  105.         asm inc bp
  106.         asm inc bp
  107.         asm movsb
  108.         asm add di,bx
  109.         asm mov si,[bp]                // si =x-ref
  110.         asm inc bp
  111.         asm inc bp
  112.         asm movsb
  113.         asm add di,bx
  114.         asm dec ah
  115.         asm jnz next_pixel
  116.         asm dec al
  117.         asm jnz next_strip
  118. done:
  119.         asm mov ax,bp
  120.         asm pop bp
  121.         asm mov ss,dx
  122.         asm mov sp,cx
  123.         asm mov nextsp,ax
  124.         asm pop bp
  125.         asm pop ds
  126.     }
  127.     asm popa
  128. }
  129.  
  130. // RUNSCROLL
  131.  
  132. void runscroll(void)
  133. {
  134.   FILE *file=fopen("LIGHTC.001","rb");
  135.   fread(sdata_ptr,1,FILELEN,file);
  136.   fclose(file);
  137.   asm mov dx,GC_INDEX+1
  138.   asm mov al,0xff
  139.   asm out dx,al
  140.     long stop,start,el;
  141.   start=timer.readtimer();
  142.   int twist=0;
  143.   word vrampos=0xa000;
  144.     long gone,herenow,timestamp[3],now=timer.readtimer();
  145.   timestamp[0]=timestamp[1]=timestamp[2]=now;
  146.   int timeroll=0;
  147.   vx.setoffset(0);
  148.   byte *line=new byte[320];
  149.   FILE *back=fopen("lightc.003","rb");
  150.   for(int y=0; y<240; y++)
  151.   {
  152.     int seg=0xa000+y*5;
  153.     fread(line,1,320,back);
  154.     for (byte plan=0; plan<4; plan++)
  155.     {
  156.       asm mov cl,plan
  157.       asm mov ax,0x0100+MAP_MASK
  158.       asm shl ah,cl
  159.       asm mov dx,SC_INDEX
  160.       asm out dx,ax
  161.       for (int teller=0; teller<80; teller++)
  162.             {
  163.     pokeb(seg,teller,line[teller*4+plan]);
  164.     pokeb(2400+seg,teller,line[teller*4+plan]);
  165.     pokeb(1200+seg,teller,line[teller*4+plan]);
  166.       }
  167.     }
  168.   }
  169.   delete line;
  170.   fclose(back);
  171.   int done=0,donereq=0;
  172.   int speed=1;
  173.   do
  174.   {
  175.         drawscroll((word *)sdata_ptr,(word *)bmp_ptr,twist*2,vrampos);
  176.         vx.setoffset((vrampos-0xa000)*16);
  177.     vrampos+=1200;
  178.         do
  179.     {
  180.       herenow=timer.readtimer();
  181.       gone=timer.elapsed(timestamp[timeroll],herenow);
  182.     } while (gone<43);
  183.     timestamp[timeroll++]=herenow;
  184.     if (timeroll==3)
  185.       timeroll=0;
  186.  
  187.     if (vrampos==44560)
  188.       vrampos=0xa000;
  189.     twist=(twist+BMPX+speed);
  190.     twist%=BMPX;
  191. #define SEGSPLIT 12
  192. #define SLICE    (BMPY*(BMPX/SEGSPLIT))
  193.         asm cld
  194.         if (keypressed[1])
  195.             donereq=1;
  196.         if ((donereq && !(twist%(BMPX/SEGSPLIT))) || done)
  197.         {
  198.       if (done<BMPX/SEGSPLIT)
  199.       {
  200.     long cl_ptr=(long)bmp_ptr;
  201.     cl_ptr+=SLICE-BMPY-done*BMPY;
  202.     asm les di,cl_ptr
  203.     for (int teller=0; teller<2*SEGSPLIT; teller++)
  204.     {
  205.       asm mov cx,BMPY/2
  206.       asm xor ax,ax
  207.       asm rep stosw
  208.       asm add di,SLICE-BMPY
  209.     }
  210.             }
  211.       done++;
  212.     }
  213.     } while (done<BMPX/SEGSPLIT);
  214.   stop=timer.readtimer();
  215.   el=timer.elapsed(start,stop);
  216. }
  217.  
  218. // SCROLLDATA
  219.  
  220. void scrolldata(void)
  221. {
  222. #if !SDATAOK
  223.   FILE *file=fopen("LIGHTC.001","wb");
  224.   vx.setoffset(0);
  225.   for (int x=-OUTRAD; x<=OUTRAD; x++)
  226.   {
  227.     int maxwidth=sqrt((long)OUTRAD*OUTRAD-(long)x*x);
  228.     for (int y=-maxwidth; y<=maxwidth; y++)
  229.     {
  230.       float radi=sqrt((long)x*x+(long)y*y);
  231.       if (radi>=INRAD)
  232.     vx.putpixel(x+CENTERX,y+CENTERY,1);
  233.     }
  234.   }
  235.   FILE *mask=fopen("LIGHTC.003","rb");
  236.   for (int y2=0; y2<240; y2++)
  237.   for (int x2=0; x2<320; x2++)
  238.     {
  239.     byte temp;
  240.     fread(&temp,1,1,mask);
  241.     if (temp)
  242.       vx.putpixel(x2,y2,0);
  243.   }
  244.   fclose(mask);
  245.     word linjer,pixels=0;
  246.   word *pos;
  247.   pos=new word[2500];
  248.     for (int plan=0; plan<4; plan++)
  249.   {
  250.     linjer=0;
  251.     for (int x=plan; x<320; x+=4)
  252.     {
  253.       int run=0;
  254.       for (int y=0; y<240; y++)
  255.       {
  256.     if (vx.getpixel(x,y)==COLOR1)
  257.     {
  258.       if (!run)
  259.         pos[linjer++]=x/4+80*y;
  260.       pixels++;
  261.       run++;
  262.       vx.putpixel(x,y,COLOR2);
  263.     }
  264.     else
  265.     {
  266.       run=0;
  267.     }
  268.       }
  269.     }
  270.         fwrite(&linjer,1,1,file);
  271.     for (int teller=0; teller<linjer; teller++)
  272.     {
  273.             int p=pos[teller];
  274.       fwrite(&p,1,2,file);
  275.       int xx=(p%80)*4+plan;
  276.       int yy=p/80;
  277.       int scan=0;
  278.       while (vx.getpixel(xx,yy+scan)==COLOR2)
  279.     scan++;
  280.             fwrite(&scan,1,1,file);
  281.       for (int teller=0; teller<scan; teller++)
  282.       {
  283.     int ref=0;
  284.     float dx=xx-CENTERX;
  285.     float dy=yy-CENTERY;
  286.     float len=sqrt(dx*dx+dy*dy)-INRAD;
  287.     float prolen=BMPY*(len/RADWIDTH);
  288.     ref=prolen;
  289.     float vin;
  290.     if (!dx)
  291.       dx=1e-5;
  292.     if (!dy)
  293.       dy=1e-5;
  294.     if (dx>=0)
  295.     {
  296.       if (dy>=0)
  297.         vin=atan(dy/dx);
  298.       else
  299.         vin=2*PI-atan(-dy/dx);
  300.     }
  301.     else
  302.     {
  303.       if (dy>=0)
  304.         vin=PI/2+atan(-dx/dy);
  305.       else
  306.         vin=PI+atan(dy/dx);
  307.     }
  308.     int slide=(vin*BMPX/(2*PI));
  309.     ref+=BMPY*slide;
  310.     fwrite(&ref,1,2,file);
  311.     yy++;
  312.       }
  313.     }
  314.   }
  315.     delete pos;
  316.   fclose(file);
  317. #endif
  318. }
  319.  
  320. // SCROLL
  321.  
  322. void scroll(void)
  323. {
  324.   sdataseg1=new byte[SDATA+32];
  325.   dataseg2=new byte[BMPDATA+32];
  326.   sdata_ptr=(byte *)((long)sdataseg1+65532l);
  327.   for (word teller=0; teller<SDATA; teller++)
  328.     sdata_ptr[teller]=0;
  329.   bmp_ptr=(byte *)((long)dataseg2+65532l);
  330.   FILE *bmpinn=fopen("LIGHTC.002","rb");
  331.   fread(bmp_ptr,1,BMPDATA,bmpinn);
  332.   for (int c=0; c<BMPX*BMPY; c++)
  333.     bmp_ptr[BMPX*BMPY+c]=bmp_ptr[c];
  334.   fclose(bmpinn);
  335. #ifndef FERDIG
  336.   scrolldata();
  337. #endif
  338.   runscroll();
  339.   delete sdataseg1;
  340.   delete dataseg2;
  341. }
  342.  
  343.